# Get machine architecture
ARCH := $(shell uname -m)

# Get kernel version
KVER = $(shell uname -r)
KMAJ = $(shell echo $(KVER) | \
sed -e 's/^\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/')
KMIN = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*.*/\1/')
KREV = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/')

kver_ge = $(shell \
echo test | awk '{if($(KMAJ) < $(1)) {print 0} else { \
if($(KMAJ) > $(1)) {print 1} else { \
if($(KMIN) < $(2)) {print 0} else { \
if($(KMIN) > $(2)) {print 1} else { \
if($(KREV) < $(3)) {print 0} else { print 1 } \
}}}}}' \
)

# Get kernel directory
KDIR:=/lib/modules/$(KVER)
DEBIAN_VERSION_FILE:=/etc/debian_version
DEBIAN_DISTRO:=$(wildcard $(DEBIAN_VERSION_FILE))

# Get current directory
PWD:=$(shell pwd)

# Get driver module directory
ifeq ($(call kver_ge,2,6,38),1)
# For kernel version >= 2.6.38
	MDIR=$(KDIR)/updates/drivers/tty/serial
else
	MDIR=$(KDIR)/kernel/drivers/serial
endif
MDIR_PP=$(KDIR)/kernel/drivers/parport

ifneq ($(findstring $(ARCH),arm aarch64),)
# For ARM/ARM64 platform
	obj-m += ax99100x.o
	obj-m += ax99100x_i2c.o
	ax99100x-objs := ax99100x_sp.o  # 移除了SPI源文件
else
# For x86/x64或其它平台
	obj-m += ax99100x.o
	obj-m += ax99100x_i2c.o
	ax99100x-objs := ax99100x_sp.o  # 移除了SPI源文件
endif

$(info current kernel version=$(KMAJ).$(KMIN).$(KREV))
$(info current serial port modules directory=$(MDIR))

default:
	$(RM) *.mod.c *.o *.ko .*.cmd *.symvers
	$(MAKE) -C $(KDIR)/build/ M=$(PWD) modules
ifneq ($(findstring $(ARCH),arm aarch64),)
	@echo "Compiling for ARM platform (without SPI support)"
else
	@echo "Compiling for non-ARM platform (without SPI support)"
endif
# 编译 serial_recv（依赖 serial_common.c）
	gcc -pthread serial_recv.c serial_utils.c serial_common.c -o serial_recv

# 编译 serial_send（不变）
	gcc -pthread serial_send.c serial_utils.c -o serial_send

# 编译 serial_recv_multi（依赖 serial_common.c，不再包含 serial_recv.c）
	gcc -pthread serial_recv_multi.c serial_utils.c serial_common.c -o serial_recv_multi

# 编译 serial_send_multi（不变）
	gcc -pthread serial_send_multi.c serial_utils.c serial_common.c -o serial_send_multi


	gcc -pthread select_BR.c -o select_BR
	gcc -pthread advanced_BR.c -o advanced_BR
	gcc -pthread gpio_99100.c -o gpio_99100
	gcc -pthread -Wall 9bit_test.c -o 9bit_test
	$(RM) *.mod.c *.o .*.cmd *.symvers *.mod
	rm -rf .tmp_version* *~
	rm -rf Module.markers modules.*
	find . -name "*.o.d" -type f -delete

install:
	@echo "Checking and unbinding driver..."
	@for dev in $$(lspci -n | grep "125b:9100" | cut -d' ' -f1); do \
		if [ -e "/sys/bus/pci/devices/0000:$$dev/driver" ]; then \
			driver_path=$$(readlink -f /sys/bus/pci/devices/0000:$$dev/driver); \
			driver=$$(basename $$driver_path); \
			echo "Found device $$dev using driver: $$driver"; \
			echo "0000:$$dev" > /sys/bus/pci/devices/0000:$$dev/driver/unbind; \
			echo "Unbound $$dev from $$driver"; \
		else \
			echo "No active driver found for device $$dev"; \
		fi \
	done
	cp ax99100x.ko $(MDIR)
	depmod -a
ifeq ($(call kver_ge,5,0,0),0)
# For kernel version < 5.0.0
	cp ax99100x.ko /etc/init.d/
	chmod +x /etc/init.d/ax99100x.ko
ifeq ($(DEBIAN_DISTRO), $(DEBIAN_VERSION_FILE))
	ln -s /etc/init.d/ax99100x.ko /etc/rcS.d/Sax99100x || true
else
	ln -s /etc/init.d/ax99100x.ko /etc/rc.d/rc3.d/Sax99100x || true  	
	ln -s /etc/init.d/ax99100x.ko /etc/rc.d/rc5.d/Sax99100x || true
endif
endif
	-modprobe ax99100x

uninstall:
	-modprobe -r ax99100x
	rm -f $(MDIR)/ax99100x.*
	depmod -a
ifeq ($(call kver_ge,5,0,0),0)
# For kernel version < 5.0.0
	rm -f /etc/init.d/ax99100x.ko
ifeq ($(DEBIAN_DISTRO), $(DEBIAN_VERSION_FILE))
	rm -f /etc/rcS.d/Sax99100x || true
else
	rm -f /etc/rc.d/rc3.d/Sax99100x
	rm -f /etc/rc.d/rc5.d/Sax99100x
endif
endif

clean:
	$(RM) *.mod.c *.o *.o.* *.ko .*.cmd *.symvers *.o.ur-safe *.mod *.o.d *.bak
	rm -rf .tmp_version* *~
	rm -rf Module.markers modules.* .cache.*
	rm -f .ax99100x_spi.o.d
	rm -f select_BR advanced_BR gpio_99100 9bit_test

    # 添加清理这三个可执行文件
	rm -f select_BR advanced_BR gpio_99100 9bit_test serial_recv serial_send serial_recv_multi

	find . -name "*.o.d" -type f -delete
	rm -f *.deb

deb:
	@echo "Building deb package..."
	@chmod +x build_deb.sh
	@./build_deb.sh